package com.schoolCommunity.util;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Component;

import java.util.Random;
import java.util.concurrent.TimeUnit;

//用户系统邮箱发送组件
@Component
public class SendEmail {

    @Autowired
    @Qualifier("MyLoginTemplate")
    private RedisTemplate redisTemplate;

    @Autowired
    JavaMailSenderImpl mailSender;

    static final String EmailSignIn = "EmailSignIn";

    static final String EmailGetPass = "EmailGetPass";

    static final String AuthorEmail = "1141649562@qq.com";

    static final String EmailTitle = "欢迎来到砚园社区";

    static final String EmailGetPassTitle = "砚园社区密码找回";


    Logger log = Logger.getLogger(SendEmail.class);

    //发送注册用验证码
    public String sendSignInEmail(String email) {

        //随机四位整数
        Random random = new Random();
        String fourRandom = random.nextInt(10000) + "";
        int randLength = fourRandom.length();
        if (randLength < 4) {
            for (int i = 1; i <= 4 - randLength; i++)
                fourRandom = "0" + fourRandom;
        }
        log.info("产生的用于注册的四位数验证码是" + fourRandom);

        try {

            log.info("获取到的邮箱是" + email);
            redisTemplate.opsForValue().set(email + EmailSignIn, fourRandom, 5, TimeUnit.MINUTES);

            //发送注册用邮箱
            SimpleMailMessage message = new SimpleMailMessage();
            message.setSubject(EmailTitle);
            message.setText("您的注册验证码是\n" + fourRandom + "\n五分钟内有效，请赶快使用");
            message.setTo(email);
            message.setFrom(AuthorEmail);
            mailSender.send(message);

        } catch (Exception e) {
            return "邮箱发送错误，请检查邮箱是否填写正确";
        }

        return "邮箱已发送";

    }

    //发送找回密码用验证码
    public String sendGetPassEmail(String email) {

        //随机四位整数
        Random random = new Random();
        String fourRandom = random.nextInt(10000) + "";
        int randLength = fourRandom.length();
        if (randLength < 4) {
            for (int i = 1; i <= 4 - randLength; i++)
                fourRandom = "0" + fourRandom;
        }
        log.info("产生的用于找回密码的四位数验证码是" + fourRandom);

        try {
            log.info("产生的用于找回密码的四位数验证码是" + fourRandom + "现在开始写入redis");
            redisTemplate.opsForValue().set(email + EmailGetPass, fourRandom, 5, TimeUnit.MINUTES);

            //发送注册用邮箱
            SimpleMailMessage message = new SimpleMailMessage();
            message.setSubject(EmailGetPassTitle);
            message.setText("您找回密码的验证是\n" + fourRandom + "\n五分钟内有效，请赶快使用");
            message.setTo(email);
            message.setFrom(AuthorEmail);
            mailSender.send(message);

        } catch (Exception e) {
            return "邮箱发送错误，请检查邮箱是否填写正确";
        }

        return "找回验证码已发送至邮箱";

    }


}
